/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package InfoMat;

import java.util.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author thomas.s
 */
public class NewsStorage {

    public class NewsItem {
        public String text;
        public String desc, img;
        public int state; // 0=normal,-1=unused,1=new,2=changed

        public NewsItem( String t, String d, String i ) { text=t; desc=d; img=i; state=1; }
        public void reset() { state=-1; }
        public void setText( String t, String d, String i ) { text=t; desc=d; img=i; state=2; }
        public boolean test( String t, String d, String im, int eq )
        {
            if (text.equals(t)) { state=0; return true; }
            int max = t.length(), count = 0;
            if (max>text.length()) max=text.length();
            for ( int i=0;i<max;i++ ) if (text.charAt(i)==t.charAt(i)) count++;
            if ( ((count*100)/max) >= eq )
            {
                state=2;
                text=t; desc=d; img=im;
                return true;
            }
            return false;
        }
    }

    public Vector title = new Vector();
    public Vector screenH = new Vector(), screenL = new Vector();
    public boolean changed = false;
    int insertPos = 0;
    String header = "";

    public NewsStorage() {  }
    public void clear( String header ) { title.removeAllElements(); title.addElement( new NewsItem(header,"","") ); insertPos=1; }
    public void setFirstRead() { changed = false; }
    public void reset( String header )
    {
        changed = false;
        insertPos = 1;
        NewsItem i = (NewsItem)title.elementAt(0);
        if (i==null) title.addElement( new NewsItem(header,"","") );
        else i.text=header;
        for ( int j=1; j<title.size(); j++ )
            ((NewsItem)(title.elementAt(j))).reset();
    }
    public void add( String t, String desc, String img )
    {
        int index = -1;
        for ( int i=1; i<title.size(); i++ )
        {
            NewsItem item = ((NewsItem)(title.elementAt(i)));
            if ( item.test(t, desc, img, 80) )
            {
                if (item.state>0) changed=true;
                index=i;
                break;
            }
        }
        if (index<0) {
            changed=true;
            title.insertElementAt( new NewsItem(t,desc,img), insertPos++ );
        }
    }

    public int mergeColors( int c1, int c2, int c3, int c4 )
    {
        return ((((c1&0xff00)+(c2&0xff00)+(c3&0xff00)+(c4&0xff00))/4)<<16)&0xff000000;
    }
    public void updateImage( int defColor, int size )
    {

        for ( int i=0; i<title.size(); i++ )
        {
            NewsItem item = ((NewsItem)(title.elementAt(i)));
            if (item.state<0) { title.removeElement(item); i--; }
        }
        
        screenH.removeAllElements();
        screenL.removeAllElements();

        System.gc();
        
        Font font;
      //  size=0;
        if (size==0) font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL); else
        if (size==1) font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE); else
        if (size==2) font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); else
        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

        int w=1024*2, h=font.getHeight()+2, color=0xffffff;
        Image line = Image.createImage(w/2,h);
        Image line2 = Image.createImage(w/2,h);

        int dw=w, dh=h;

        if (size<2) { dw=w/2; dh=h/2; }
        else { dw=w/2; dh=h; }

        //dw=w; dh=h;
        int src[] = new int[w*h];
        int dst[] = new int[dw*dh];
        int dstL[] = new int[dh*dw];


        Graphics g = line.getGraphics();
        Graphics g2 = line2.getGraphics();
        g.setFont(font);
        g2.setFont(font);

        for ( int i=0; i<title.size(); i++ )
        {
            NewsItem item = ((NewsItem)(title.elementAt(i)));
            g.setColor(0);
            g2.setColor(0);
            g.fillRect(0,0,w/2,h);
            g2.fillRect(0,0,w/2,h);
            g.setColor( (item.state<0 ? 0x404040 : (item.state>0 ? 0xffffff : 0xa0a0a0)) );
            g2.setColor( (item.state<0 ? 0x404040 : (item.state>0 ? 0xffffff : 0xa0a0a0)) );
            g.drawString( item.text, 2, 2, 0);
            g2.drawString( item.text, 2-w/2, 2, 0);

            line.getRGB(src, 0, w, 0, 0, w/2, h);
            line2.getRGB(src, w/2, w, 0, 0, w/2, h);
            color = (i==0)?0xffffff:(defColor&0xffffff);

           /* if (dh==h && dw==w)
            {   for ( int y=0, ofs=0; y<dh; y++ ) for ( int x=0; x<dw; x++, ofs++ )
                    dstL[(dh-y-1)+x*dh] = dst[x+y*dw] = ((src[ofs]&0xff00)<<16)|color;
            } 
            else*/
            if (dw==w/2 && dh==h) {
                for ( int y=0; y<dh; y++ ) for ( int x=0, ofs=y*w; x<dw; x++, ofs++ )
                    dstL[(dh-y-1)+x*dh] = dst[x+y*dw] = ((src[ofs]&0xff)<<24)|color;
            } else {
                for ( int y=0; y<dh; y++ ) for ( int x=0, ofs=y*2*w; x<dw; x++, ofs+=2 )
                    dstL[(dh-y-1)+x*dh] = dst[x+y*dw] = mergeColors( src[ofs], src[ofs+1], src[ofs+w], src[ofs+w+1])|color;
            }
            screenH.addElement( Image.createRGBImage(dst, dw, dh, true) );
            screenL.addElement( Image.createRGBImage(dstL, dh, dw, true) );
        }
    }

}
